package leetcode_1_200;

public class LeeCode_134_2 {
    public static void main(String[] args) {
        System.out.println(canCompleteCircuit(new int[]{1, 2, 3, 4, 5}, new int[]{3, 4, 5, 1, 2}));
        System.out.println(canCompleteCircuit(new int[]{2,3,4}, new int[]{3, 4, 3}));
        System.out.println(canCompleteCircuit(new int[]{3,1,1}, new int[]{1,2,2}));
    }

    private static int canCompleteCircuit(int[] gas, int[] cost) {
        int n = gas.length;
        int i = 0;
        while (i < n){
            int hav = 0;
            int cnt = 0;
            int cos = 0;
            while (cnt < n){
                hav += gas[(cnt + i) % n];
                cos += cost[(cnt + i) % n];
                if (hav < cos){
                    break;
                }
                cnt++;
            }
            if (cnt == n)
                return i;
            i = i + cnt + 1;
        }
        return -1;
    }
}
